package dmwr.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dmwr.Dmwr;
import dmwr.web.EntryAdmin;
import dmwr.web.Http;
import dmwr.web.PoolAdmin;
import dmwr.web.UserAdmin;

public class Admin extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private final UserAdmin userAdmin = new UserAdmin();
	private final PoolAdmin poolAdmin = new PoolAdmin();
	private final EntryAdmin entryAdmin = new EntryAdmin();

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String uri = Http.request().getRequestURI();
		if (uri.endsWith("/")) {
			uri = uri.substring(0, uri.length() - 1);
		}
		if ("/admin".equals(uri)) {
			admin();
		} else if ("/admin/user".equals(uri)) {
			user();
		} else if ("/admin/pool".equals(uri)) {
			pool();
		} else if ("/admin/entry".equals(uri)) {
			entry();
		} else if ("/admin/reset".equals(uri)) {
			Dmwr.service().reset();
		}
	}

	private void admin() {
		Http.forward("/admin/index.jsp");
	}

	private void user() {
		if (Http.request().getParameter("add") != null) {
			userAdmin.add();
		} else if (Http.request().getParameter("edit") != null) {
			userAdmin.edit();
		} else if (Http.request().getParameter("save") != null) {
			userAdmin.save();
		} else if (Http.request().getParameter("delete") != null) {
			userAdmin.delete();
		} else {
			userAdmin.list();
		}
	}

	private void pool() {
		if (Http.request().getParameter("add") != null) {
			poolAdmin.add();
		} else if (Http.request().getParameter("edit") != null) {
			poolAdmin.edit();
		} else if (Http.request().getParameter("save") != null) {
			poolAdmin.save();
		} else if (Http.request().getParameter("delete") != null) {
			poolAdmin.delete();
		} else {
			poolAdmin.list();
		}
	}

	private void entry() {
		if (Http.request().getParameter("add") != null) {
			entryAdmin.add();
		} else if (Http.request().getParameter("edit") != null) {
			entryAdmin.edit();
		} else if (Http.request().getParameter("save") != null) {
			entryAdmin.save();
		} else if (Http.request().getParameter("delete") != null) {
			entryAdmin.delete();
			// } else if (request.getParameter("addAll") != null) {
			// InputStream in = getClass().getClassLoader()
			// .getResourceAsStream("entries.txt");
			// try {
			// BufferedReader r = new BufferedReader(
			// new InputStreamReader(in, "UTF-8"));
			// String l;
			// while ((l = r.readLine()) != null) {
			// int i = l.indexOf('=');
			// Dmwr.service().saveEntry(
			// new Entry(null, l.substring(i + 1), l
			// .substring(0, i), null));
			// Thread.sleep(2);
			// }
			// } catch (Exception e) {
			// throw new RuntimeException(e);
			// } finally {
			// try {
			// in.close();
			// } catch (IOException e) {
			// }
			// }
			// } else if (Http.request().getParameter("foo") != null) {
			// Dmwr.service().addAllEntriesToPool("Tedesco");
		} else {
			entryAdmin.list();
		}
	}
}
